home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / emacs-complete / fsf / emacs / lisp / cmuscheme.el < prev    next >
Lisp/Scheme  |  1994-05-03  |  19KB  |  457 lines

  1. ;;; cmuscheme.el --- Scheme process in a buffer. Adapted from tea.el.
  2.  
  3. ;; Copyright (C) 1988, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Olin Shivers <olin.shivers@cs.cmu.edu>
  6. ;; Maintainer: FSF
  7. ;; Keywords: processes, lisp
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  11. ;; GNU Emacs is free software; you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. ;; GNU General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  23. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. ;;; Commentary:
  26.  
  27. ;;;    This is a customisation of comint-mode (see comint.el)
  28. ;;;
  29. ;;; Written by Olin Shivers (olin.shivers@cs.cmu.edu). With bits and pieces
  30. ;;; lifted from scheme.el, shell.el, clisp.el, newclisp.el, cobol.el, et al..
  31. ;;; 8/88
  32. ;;;
  33. ;;; Please send me bug reports, bug fixes, and extensions, so that I can
  34. ;;; merge them into the master source.
  35. ;;;
  36. ;;; The changelog is at the end of this file.
  37. ;;;
  38. ;;; NOTE: MIT Cscheme, when invoked with the -emacs flag, has a special user
  39. ;;; interface that communicates process state back to the superior emacs by
  40. ;;; outputting special control sequences. The gnumacs package, xscheme.el, has
  41. ;;; lots and lots of special purpose code to read these control sequences, and
  42. ;;; so is very tightly integrated with the cscheme process. The cscheme
  43. ;;; interrupt handler and debugger read single character commands in cbreak
  44. ;;; mode; when this happens, xscheme.el switches to special keymaps that bind
  45. ;;; the single letter command keys to emacs functions that directly send the
  46. ;;; character to the scheme process.  Cmuscheme mode does *not* provide this
  47. ;;; functionality. If you are a cscheme user, you may prefer to use the
  48. ;;; xscheme.el/cscheme -emacs interaction.
  49. ;;; 
  50. ;;; Here's a summary of the pros and cons, as I see them.
  51. ;;; xscheme: Tightly integrated with inferior cscheme process!  A few commands
  52. ;;;         not in cmuscheme. But. Integration is a bit of a hack.  Input
  53. ;;;         history only keeps the immediately prior input. Bizarre
  54. ;;;         keybindings.
  55. ;;; 
  56. ;;; cmuscheme: Not tightly integrated with inferior cscheme process.  But.
  57. ;;;            Carefully integrated functionality with the entire suite of
  58. ;;;            comint-derived CMU process modes. Keybindings reminiscent of
  59. ;;;            Zwei and Hemlock. Good input history. A few commands not in
  60. ;;;            xscheme.
  61. ;;;  
  62. ;;; It's a tradeoff. Pay your money; take your choice. If you use a Scheme
  63. ;;; that isn't Cscheme, of course, there isn't a choice. Xscheme.el is *very*
  64. ;;; Cscheme-specific; you must use cmuscheme.el.  Interested parties are
  65. ;;; invited to port xscheme functionality on top of comint mode...
  66.  
  67. ;; YOUR .EMACS FILE
  68. ;;=============================================================================
  69. ;; Some suggestions for your .emacs file.
  70. ;;
  71. ;; ; If cmuscheme lives in some non-standard directory, you must tell emacs
  72. ;; ; where to get it. This may or may not be necessary.
  73. ;; (setq load-path (cons (expand-file-name "~jones/lib/emacs") load-path))
  74. ;;
  75. ;; ; Autoload run-scheme from file cmuscheme.el
  76. ;; (autoload 'run-scheme "cmuscheme"
  77. ;;           "Run an inferior Scheme process."
  78. ;;           t)
  79. ;;
  80. ;; ; Files ending in ".scm" are Scheme source, 
  81. ;; ; so put their buffers in scheme-mode.
  82. ;; (setq auto-mode-alist 
  83. ;;       (cons '("\\.scm$" . scheme-mode)  
  84. ;;             auto-mode-alist))
  85. ;;
  86. ;; ; Define C-c t to run my favorite command in inferior scheme mode:
  87. ;; (setq cmuscheme-load-hook
  88. ;;       '((lambda () (define-key inferior-scheme-mode-map "\C-ct"
  89. ;;                                'favorite-cmd))))
  90. ;;;
  91. ;;; Unfortunately, scheme.el defines run-scheme to autoload from xscheme.el.
  92. ;;; This will womp your declaration to autoload run-scheme from cmuscheme.el
  93. ;;; if you haven't loaded cmuscheme in before scheme. Three fixes:
  94. ;;; - Put the autoload on your scheme mode hook and in your .emacs toplevel:
  95. ;;;   (setq scheme-mode-hook
  96. ;;;         '((lambda () (autoload 'run-scheme "cmuscheme"
  97. ;;;                                "Run an inferior Scheme" t))))
  98. ;;;   (autoload 'run-scheme "cmuscheme" "Run an inferior Scheme" t)
  99. ;;;   Now when scheme.el autoloads, it will restore the run-scheme autoload.
  100. ;;; - Load cmuscheme.el in your .emacs: (load-library 'cmuscheme)
  101. ;;; - Change autoload declaration in scheme.el to point to cmuscheme.el:
  102. ;;;   (autoload 'run-scheme "cmuscheme" "Run an inferior Scheme" t)
  103. ;;;   *or* just delete the autoload declaration from scheme.el altogether,
  104. ;;;   which will allow the autoload in your .emacs to have its say.
  105.  
  106. ;;; Code:
  107.  
  108. (require 'scheme)
  109. (require 'comint)
  110.  
  111. ;;; INFERIOR SCHEME MODE STUFF
  112. ;;;============================================================================
  113.  
  114. (defvar inferior-scheme-mode-hook nil
  115.   "*Hook for customising inferior-scheme mode.")
  116. (defvar inferior-scheme-mode-map nil)
  117.  
  118. (cond ((not inferior-scheme-mode-map)
  119.        (setq inferior-scheme-mode-map
  120.          (copy-keymap comint-mode-map))
  121.        (define-key inferior-scheme-mode-map "\M-\C-x" ;gnu convention
  122.                'scheme-send-definition)
  123.        (define-key inferior-scheme-mode-map "\C-x\C-e" 'scheme-send-last-sexp)
  124.        (define-key inferior-scheme-mode-map "\C-c\C-l" 'scheme-load-file)
  125.        (define-key inferior-scheme-mode-map "\C-c\C-k" 'scheme-compile-file)
  126.        (scheme-mode-commands inferior-scheme-mode-map))) 
  127.  
  128. ;; Install the process communication commands in the scheme-mode keymap.
  129. (define-key scheme-mode-map "\M-\C-x" 'scheme-send-definition);gnu convention
  130. (define-key scheme-mode-map "\C-x\C-e" 'scheme-send-last-sexp);gnu convention
  131. (define-key scheme-mode-map "\C-c\C-e" 'scheme-send-definition)
  132. (define-key scheme-mode-map "\C-c\M-e" 'scheme-send-definition-and-go)
  133. (define-key scheme-mode-map "\C-c\C-r" 'scheme-send-region)
  134. (define-key scheme-mode-map "\C-c\M-r" 'scheme-send-region-and-go)
  135. (define-key scheme-mode-map "\C-c\M-c" 'scheme-compile-definition)
  136. (define-key scheme-mode-map "\C-c\C-c" 'scheme-compile-definition-and-go)
  137. (define-key scheme-mode-map "\C-c\C-z" 'switch-to-scheme)
  138. (define-key scheme-mode-map "\C-c\C-l" 'scheme-load-file)
  139. (define-key scheme-mode-map "\C-c\C-k" 'scheme-compile-file) ;k for "kompile"
  140.  
  141. (defvar scheme-buffer)
  142.  
  143. (defun inferior-scheme-mode ()
  144.   "Major mode for interacting with an inferior Scheme process.
  145.  
  146. The following commands are available:
  147. \\{inferior-scheme-mode-map}
  148.  
  149. A Scheme process can be fired up with M-x run-scheme.
  150.  
  151. Customisation: Entry to this mode runs the hooks on comint-mode-hook and
  152. inferior-scheme-mode-hook (in that order).
  153.  
  154. You can send text to the inferior Scheme process from other buffers containing
  155. Scheme source.  
  156.     switch-to-scheme switches the current buffer to the Scheme process buffer.
  157.     scheme-send-definition sends the current definition to the Scheme process.
  158.     scheme-compile-definition compiles the current definition.
  159.     scheme-send-region sends the current region to the Scheme process.
  160.     scheme-compile-region compiles the current region.
  161.  
  162.     scheme-send-definition-and-go, scheme-compile-definition-and-go,
  163.         scheme-send-region-and-go, and scheme-compile-region-and-go
  164.         switch to the Scheme process buffer after sending their text.
  165. For information on running multiple processes in multiple buffers, see
  166. documentation for variable scheme-buffer.
  167.  
  168. Commands:
  169. Return after the end of the process' output sends the text from the 
  170.     end of process to point.
  171. Return before the end of the process' output copies the sexp ending at point
  172.     to the end of the process' output, and sends it.
  173. Delete converts tabs to spaces as it moves back.
  174. Tab indents for Scheme; with argument, shifts rest
  175.     of expression rigidly with the current line.
  176. C-M-q does Tab on each line starting within following expression.
  177. Paragraphs are separated only by blank lines.  Semicolons start comments.
  178. If you accidentally suspend your process, use \\[comint-continue-subjob]
  179. to continue it."
  180.   (interactive)
  181.   (comint-mode)
  182.   ;; Customise in inferior-scheme-mode-hook
  183.   (setq comint-prompt-regexp "^[^>\n]*>+ *") ; OK for cscheme, oaklisp, T,...
  184.   (scheme-mode-variables)
  185.   (setq major-mode 'inferior-scheme-mode)
  186.   (setq mode-name "Inferior Scheme")
  187.   (setq mode-line-process '(":%s"))
  188.   (use-local-map inferior-scheme-mode-map)
  189.   (setq comint-input-filter (function scheme-input-filter))
  190.   (setq comint-get-old-input (function scheme-get-old-input))
  191.   (run-hooks 'inferior-scheme-mode-hook))
  192.  
  193. (defvar inferior-scheme-filter-regexp "\\`\\s *\\S ?\\S ?\\s *\\'"
  194.   "*Input matching this regexp are not saved on the history list.
  195. Defaults to a regexp ignoring all inputs of 0, 1, or 2 letters.")
  196.  
  197. (defun scheme-input-filter (str)
  198.   "Don't save anything matching inferior-scheme-filter-regexp"
  199.   (not (string-match inferior-scheme-filter-regexp str)))
  200.  
  201. (defun scheme-get-old-input ()
  202.   "Snarf the sexp ending at point"
  203.   (save-excursion
  204.     (let ((end (point)))
  205.       (backward-sexp)
  206.       (buffer-substring (point) end))))
  207.  
  208. (defun scheme-args-to-list (string)
  209.   (let ((where (string-match "[ \t]" string)))
  210.     (cond ((null where) (list string))
  211.       ((not (= where 0))
  212.        (cons (substring string 0 where)
  213.          (scheme-args-to-list (substring string (+ 1 where)
  214.                          (length string)))))
  215.       (t (let ((pos (string-match "[^ \t]" string)))
  216.            (if (null pos)
  217.            nil
  218.          (scheme-args-to-list (substring string pos
  219.                          (length string)))))))))
  220.  
  221. (defvar scheme-program-name "scheme"
  222.   "*Program invoked by the run-scheme command")
  223.  
  224. ;;; Obsolete
  225. (defun scheme (&rest foo)
  226.   "Use run-scheme"
  227.   (interactive)
  228.   (message "Use run-scheme")
  229.   (ding))
  230.  
  231. (defun run-scheme (cmd)
  232.   "Run an inferior Scheme process, input and output via buffer *scheme*.
  233. If there is a process already running in *scheme*, just switch to that buffer.
  234. With argument, allows you to edit the command line (default is value
  235. of scheme-program-name).  Runs the hooks from inferior-scheme-mode-hook
  236. \(after the comint-mode-hook is run).
  237. \(Type \\[describe-mode] in the process buffer for a list of commands.)"
  238.  
  239.   (interactive (list (if current-prefix-arg
  240.              (read-string "Run Scheme: " scheme-program-name)
  241.              scheme-program-name)))
  242.   (if (not (comint-check-proc "*scheme*"))
  243.       (let ((cmdlist (scheme-args-to-list cmd)))
  244.     (set-buffer (apply 'make-comint "scheme" (car cmdlist)
  245.                nil (cdr cmdlist)))
  246.     (inferior-scheme-mode)))
  247.   (setq scheme-buffer "*scheme*")
  248.   (switch-to-buffer "*scheme*"))
  249.  
  250.  
  251. (defun scheme-send-region (start end)
  252.   "Send the current region to the inferior Scheme process."
  253.   (interactive "r")
  254.   (comint-send-region (scheme-proc) start end)
  255.   (comint-send-string (scheme-proc) "\n"))
  256.  
  257. (defun scheme-send-definition ()
  258.   "Send the current definition to the inferior Scheme process."
  259.   (interactive)
  260.   (save-excursion
  261.    (end-of-defun)
  262.    (let ((end (point)))
  263.      (beginning-of-defun)
  264.      (scheme-send-region (point) end))))
  265.  
  266. (defun scheme-send-last-sexp ()
  267.   "Send the previous sexp to the inferior Scheme process."
  268.   (interactive)
  269.   (scheme-send-region (save-excursion (backward-sexp) (point)) (point)))
  270.  
  271. (defvar scheme-compile-exp-command "(compile '%s)"
  272.   "*Template for issuing commands to compile arbitrary Scheme expressions.")
  273.  
  274. (defun scheme-compile-region (start end)
  275.   "Compile the current region in the inferior Scheme process.
  276. \(A BEGIN is wrapped around the region: (BEGIN <region>))"
  277.   (interactive "r")
  278.   (comint-send-string (scheme-proc) (format scheme-compile-exp-command
  279.                         (format "(begin %s)"
  280.                             (buffer-substring start end))))
  281.   (comint-send-string (scheme-proc) "\n"))
  282.  
  283. (defun scheme-compile-definition ()
  284.   "Compile the current definition in the inferior Scheme process."
  285.   (interactive)
  286.   (save-excursion
  287.    (end-of-defun)
  288.    (let ((end (point)))
  289.      (beginning-of-defun)
  290.      (scheme-compile-region (point) end))))
  291.  
  292. (defun switch-to-scheme (eob-p)
  293.   "Switch to the scheme process buffer.
  294. With argument, positions cursor at end of buffer."
  295.   (interactive "P")
  296.   (if (get-buffer scheme-buffer)
  297.       (pop-to-buffer scheme-buffer)
  298.       (error "No current process buffer. See variable scheme-buffer."))
  299.   (cond (eob-p
  300.      (push-mark)
  301.      (goto-char (point-max)))))
  302.  
  303. (defun scheme-send-region-and-go (start end)
  304.   "Send the current region to the inferior Scheme process.
  305. Then switch to the process buffer."
  306.   (interactive "r")
  307.   (scheme-send-region start end)
  308.   (switch-to-scheme t))
  309.  
  310. (defun scheme-send-definition-and-go ()
  311.   "Send the current definition to the inferior Scheme. 
  312. Then switch to the process buffer."
  313.   (interactive)
  314.   (scheme-send-definition)
  315.   (switch-to-scheme t))
  316.  
  317. (defun scheme-compile-definition-and-go ()
  318.   "Compile the current definition in the inferior Scheme. 
  319. Then switch to the process buffer."
  320.   (interactive)
  321.   (scheme-compile-definition)
  322.   (switch-to-scheme t))
  323.  
  324. (defun scheme-compile-region-and-go (start end)
  325.   "Compile the current region in the inferior Scheme. 
  326. Then switch to the process buffer."
  327.   (interactive "r")
  328.   (scheme-compile-region start end)
  329.   (switch-to-scheme t))
  330.  
  331. (defvar scheme-source-modes '(scheme-mode)
  332.   "*Used to determine if a buffer contains Scheme source code.
  333. If it's loaded into a buffer that is in one of these major modes, it's
  334. considered a scheme source file by scheme-load-file and scheme-compile-file.
  335. Used by these commands to determine defaults.")
  336.  
  337. (defvar scheme-prev-l/c-dir/file nil
  338.   "Caches the last (directory . file) pair.
  339. Caches the last pair used in the last scheme-load-file or
  340. scheme-compile-file command. Used for determining the default in the 
  341. next one.")
  342.  
  343. (defun scheme-load-file (file-name)
  344.   "Load a Scheme file into the inferior Scheme process."
  345.   (interactive (comint-get-source "Load Scheme file: " scheme-prev-l/c-dir/file
  346.                   scheme-source-modes t)) ; T because LOAD 
  347.                                                           ; needs an exact name
  348.   (comint-check-source file-name) ; Check to see if buffer needs saved.
  349.   (setq scheme-prev-l/c-dir/file (cons (file-name-directory    file-name)
  350.                        (file-name-nondirectory file-name)))
  351.   (comint-send-string (scheme-proc) (concat "(load \""
  352.                         file-name
  353.                         "\"\)\n")))
  354.  
  355. (defun scheme-compile-file (file-name)
  356.   "Compile a Scheme file in the inferior Scheme process."
  357.   (interactive (comint-get-source "Compile Scheme file: "
  358.                   scheme-prev-l/c-dir/file
  359.                   scheme-source-modes
  360.                   nil)) ; NIL because COMPILE doesn't
  361.                                         ; need an exact name.
  362.   (comint-check-source file-name) ; Check to see if buffer needs saved.
  363.   (setq scheme-prev-l/c-dir/file (cons (file-name-directory    file-name)
  364.                        (file-name-nondirectory file-name)))
  365.   (comint-send-string (scheme-proc) (concat "(compile-file \""
  366.                         file-name
  367.                         "\"\)\n")))
  368.  
  369.  
  370. (defvar scheme-buffer nil "*The current scheme process buffer.
  371.  
  372. MULTIPLE PROCESS SUPPORT
  373. ===========================================================================
  374. Cmuscheme.el supports, in a fairly simple fashion, running multiple Scheme
  375. processes. To run multiple Scheme processes, you start the first up with
  376. \\[run-scheme]. It will be in a buffer named *scheme*. Rename this buffer
  377. with \\[rename-buffer]. You may now start up a new process with another
  378. \\[run-scheme]. It will be in a new buffer, named *scheme*. You can
  379. switch between the different process buffers with \\[switch-to-buffer].
  380.  
  381. Commands that send text from source buffers to Scheme processes --
  382. like scheme-send-definition or scheme-compile-region -- have to choose a
  383. process to send to, when you have more than one Scheme process around. This
  384. is determined by the global variable scheme-buffer. Suppose you
  385. have three inferior Schemes running:
  386.     Buffer    Process
  387.     foo        scheme
  388.     bar        scheme<2>
  389.     *scheme*    scheme<3>
  390. If you do a \\[scheme-send-definition-and-go] command on some Scheme source
  391. code, what process do you send it to?
  392.  
  393. - If you're in a process buffer (foo, bar, or *scheme*), 
  394.   you send it to that process.
  395. - If you're in some other buffer (e.g., a source file), you
  396.   send it to the process attached to buffer scheme-buffer.
  397. This process selection is performed by function scheme-proc.
  398.  
  399. Whenever \\[run-scheme] fires up a new process, it resets scheme-buffer
  400. to be the new process's buffer. If you only run one process, this will
  401. do the right thing. If you run multiple processes, you can change
  402. scheme-buffer to another process buffer with \\[set-variable].
  403.  
  404. More sophisticated approaches are, of course, possible. If you find yourself
  405. needing to switch back and forth between multiple processes frequently,
  406. you may wish to consider ilisp.el, a larger, more sophisticated package
  407. for running inferior Lisp and Scheme processes. The approach taken here is
  408. for a minimal, simple implementation. Feel free to extend it.")
  409.  
  410. (defun scheme-proc ()
  411.   "Returns the current scheme process. See variable scheme-buffer."
  412.   (let ((proc (get-buffer-process (if (eq major-mode 'inferior-scheme-mode)
  413.                       (current-buffer)
  414.                       scheme-buffer))))
  415.     (or proc
  416.     (error "No current process. See variable scheme-buffer"))))
  417.  
  418.  
  419. ;;; Do the user's customisation...
  420.  
  421. (defvar cmuscheme-load-hook nil
  422.   "This hook is run when cmuscheme is loaded in.
  423. This is a good place to put keybindings.")
  424.     
  425. (run-hooks 'cmuscheme-load-hook)
  426.  
  427.  
  428. ;;; CHANGE LOG
  429. ;;; ===========================================================================
  430. ;;; 8/88 Olin
  431. ;;; Created. 
  432. ;;;
  433. ;;; 2/15/89 Olin
  434. ;;; Removed -emacs flag from process invocation. It's only useful for
  435. ;;; cscheme, and makes cscheme assume it's running under xscheme.el,
  436. ;;; which messes things up royally. A bug.
  437. ;;;
  438. ;;; 5/22/90 Olin
  439. ;;; - Upgraded to use comint-send-string and comint-send-region.
  440. ;;; - run-scheme now offers to let you edit the command line if
  441. ;;;   you invoke it with a prefix-arg. M-x scheme is redundant, and
  442. ;;;   has been removed.
  443. ;;; - Explicit references to process "scheme" have been replaced with
  444. ;;;   (scheme-proc). This allows better handling of multiple process bufs.
  445. ;;; - Added scheme-send-last-sexp, bound to C-x C-e. A gnu convention.
  446. ;;; - Have not added process query facility a la cmulisp.el's lisp-show-arglist
  447. ;;;   and friends, but interested hackers might find a useful application
  448. ;;;   of this facility.
  449. ;;;
  450. ;;; 3/12/90 Olin
  451. ;;; - scheme-load-file and scheme-compile-file no longer switch-to-scheme.
  452. ;;;   Tale suggested this.
  453.  
  454. (provide 'cmuscheme)
  455.  
  456. ;;; cmuscheme.el ends here
  457.